package com.cxy.datawarehouse.service;

import com.cxy.datawarehouse.entity.StandardCode;
import com.cxy.datawarehouse.mapper.StandardCodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class StandardCodeService {
    @Resource
    StandardCodeMapper standardCodeMapper;

    public List<StandardCode> list(){
        return standardCodeMapper.findAll();
    }

    public StandardCode getById(Long id){
        return standardCodeMapper.getStandardCodeByStandardCodeId(id);
    }

    public List<StandardCode> getByTypeId(Long id){
        return standardCodeMapper.getStandardCodesByClassId(id);
    }

    public void update(StandardCode standardCode){
        standardCodeMapper.save(standardCode);
    }

    public void create(StandardCode standardCode){
        standardCodeMapper.save(standardCode);
    }

    public void stopById(Long id){
        Optional<StandardCode> optionalStandardCode = standardCodeMapper.findById(id);
        if (!optionalStandardCode.isPresent()){
            return;
        }
        StandardCode standardCode = optionalStandardCode.get();
        standardCode.setStatus((long) 0);
        standardCodeMapper.save(standardCode);
    }

    public void openById(Long id){
        Optional<StandardCode> optionalStandardCode = standardCodeMapper.findById(id);
        if (!optionalStandardCode.isPresent()){
            return;
        }
        StandardCode standardCode = optionalStandardCode.get();
        standardCode.setStatus((long) 1);
        standardCodeMapper.save(standardCode);
    }

    public void deleteById(Long id){
        standardCodeMapper.deleteById(id);
    }

}
